package org.dromara.hm.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.hm.domain.bo.HmSaleOrderRefundBo;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderRefundVo;
import org.dromara.hm.domain.HmSaleOrderRefund;
import org.dromara.hm.mapper.saleOrder.HmSaleOrderRefundMapper;
import org.dromara.hm.service.IHmSaleOrderRefundService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 退款退货明细Service业务层处理
 *
 * @author Hzy
 * @date 2025-04-10
 */
@RequiredArgsConstructor
@Service
public class HmSaleOrderRefundServiceImpl implements IHmSaleOrderRefundService {

    private final HmSaleOrderRefundMapper baseMapper;

    /**
     * 查询退款退货明细
     *
     * @param refundId 主键
     * @return 退款退货明细
     */
    @Override
    public HmSaleOrderRefundVo queryById(String refundId){
        return baseMapper.selectVoById(refundId);
    }

    /**
     * 分页查询退款退货明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 退款退货明细分页列表
     */
    @Override
    public TableDataInfo<HmSaleOrderRefundVo> queryPageList(HmSaleOrderRefundBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmSaleOrderRefund> lqw = buildQueryWrapper(bo);
        Page<HmSaleOrderRefundVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的退款退货明细列表
     *
     * @param bo 查询条件
     * @return 退款退货明细列表
     */
    @Override
    public List<HmSaleOrderRefundVo> queryList(HmSaleOrderRefundBo bo) {
        LambdaQueryWrapper<HmSaleOrderRefund> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmSaleOrderRefund> buildQueryWrapper(HmSaleOrderRefundBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmSaleOrderRefund> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HmSaleOrderRefund::getRefundId);
        lqw.eq(bo.getRefundId()!= null, HmSaleOrderRefund::getRefundId, bo.getRefundId());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleId()), HmSaleOrderRefund::getSaleId, bo.getSaleId());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleProductId()), HmSaleOrderRefund::getSaleProductId, bo.getSaleProductId());
        lqw.eq(bo.getRefundType() != null, HmSaleOrderRefund::getRefundType, bo.getRefundType());
        lqw.eq(bo.getRefundAmount() != null, HmSaleOrderRefund::getRefundAmount, bo.getRefundAmount());
        lqw.eq(bo.getActualReturnToStockNum() != null, HmSaleOrderRefund::getActualReturnToStockNum, bo.getActualReturnToStockNum());
        lqw.eq(bo.getReturnNum() != null, HmSaleOrderRefund::getReturnNum, bo.getReturnNum());
        lqw.eq(bo.getApplyTime() != null, HmSaleOrderRefund::getApplyTime, bo.getApplyTime());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessorId()), HmSaleOrderRefund::getProcessorId, bo.getProcessorId());
        lqw.eq(bo.getProcessTime() != null, HmSaleOrderRefund::getProcessTime, bo.getProcessTime());
        lqw.eq(bo.getStatus() != null, HmSaleOrderRefund::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getReasonCode()), HmSaleOrderRefund::getReasonCode, bo.getReasonCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), HmSaleOrderRefund::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getProofFiles()), HmSaleOrderRefund::getProofFiles, bo.getProofFiles());
        return lqw;
    }

    /**
     * 新增退款退货明细
     *
     * @param bo 退款退货明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HmSaleOrderRefundBo bo) {
        HmSaleOrderRefund add = MapstructUtils.convert(bo, HmSaleOrderRefund.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRefundId(add.getRefundId());
        }
        return flag;
    }

    /**
     * 修改退款退货明细
     *
     * @param bo 退款退货明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HmSaleOrderRefundBo bo) {
        HmSaleOrderRefund update = MapstructUtils.convert(bo, HmSaleOrderRefund.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmSaleOrderRefund entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除退款退货明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
